/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "radiobutton.h"

static const char class_name [] = "EGUI Widget RadioButton";

static int afc_easygui_internal_radiobutton_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_radiobutton ( DynamicClass * dyn );

static void afc_easygui_radiobutton_event_toggled ( GtkWidget * w, gpointer data );

// Methods
static int afc_easygui_radiobutton_method_get_value ( DynamicClass * dc );
static int afc_easygui_radiobutton_method_set_value ( DynamicClass * dc );

DynamicClass * afc_easygui_internal_radiobutton_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynamic class: radiobutton" );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_radiobutton_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_radiobutton );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "get_value", NULL,  afc_easygui_radiobutton_method_get_value );
	afc_dynamic_class_add_method ( dyn, "set_value", NULL,  afc_easygui_radiobutton_method_set_value );

	return ( dyn );
}

int afc_easygui_internal_radiobutton_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	/*
	if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
	*/
}

static int afc_easygui_internal_radiobutton_get_template ( DynamicClass * dyn )
{
	dyn->result = "GROUP/A TEXT ACTION VALUE";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_radiobutton ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	char * label = NULL;
	int afc_res = AFC_ERR_NO_ERROR;
	char * groupid = NULL;
	char * action;
	char * value;
	GSList * group = NULL;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	action 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTION" );
	value 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "VALUE" );

	if ( ( groupid = afc_cmd_parser_arg_get_by_name ( egui->parser, "GROUP" ) ) == NULL ) groupid = "1";
	group = afc_dictionary_get ( context->radiogroups, groupid );


	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "radiobutton" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );
	
	WIDGET_SET_CONTEXT ( dc, context );

	if ( ( label = afc_cmd_parser_arg_get_by_name ( egui->parser, "TEXT") ) != NULL )
		w = gtk_radio_button_new_with_label ( group, label );
	else w = gtk_radio_button_new ( group );

	group = gtk_radio_button_group ( GTK_RADIO_BUTTON ( w ) );
	afc_dictionary_set ( context->radiogroups, groupid, group );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );

	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "toggled", action, NULL, (GtkSignalFunc) afc_easygui_radiobutton_event_toggled ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );

	if ( value ) afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_STRING, "radio_value", value );


	return ( afc_res );
}

// EVENT FUNCTIONS
static void afc_easygui_radiobutton_event_toggled ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIRadioSignalFunc sig;
	short active;
	char * value;

	WIDGET_GET_CALLBACK_CAST ( dc, "toggled", sig, EasyGUIRadioSignalFunc );

	active = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON ( w ) );
	value  = afc_dynamic_class_get_var ( dc, "radio_value" );

	sig ( dc, WIDGET_GET_CONTEXT ( dc ), active, value );
}

// METHODS
static int afc_easygui_radiobutton_method_set_value ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget;
	gboolean active;

	WIDGET_GET_EWIDGET ( dc, ewidget );
	
	active = ( gboolean ) ( int ) afc_array_master_first ( dc->args );

	gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( ewidget->gtk_handle ), active );

	return ( AFC_ERR_NO_ERROR );	
}

// Gets the toggle button value
// Result is stored in dc->result
static int afc_easygui_radiobutton_method_get_value ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget;

	WIDGET_GET_EWIDGET ( dc, ewidget );

	dc->result = ( void * ) gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON ( ewidget->gtk_handle ) );

	return ( AFC_ERR_NO_ERROR );	
}

